<template>
  <div class="container">
    <Header v-if="isHome" v-bind:isBack="isBack" :title="title"></Header>
    <!-- 头部选手信息 -->
    <div class="top-card">
      <div class="left">
        <div class="img">
          <img :src="matchObj.user_avator" alt />
        </div>
        <div class="name fs-32">{{matchObj.number}}号.{{matchObj.name}}</div>
      </div>
      <div class="right fs-28">{{matchObj.game_area}}</div>
    </div>
    <!-- 中间的数据 -->
    <div class="center-option">
      <div class="top">
        <div class="item">
          <div class="num">{{matchObj.vote_num}}</div>
          <div class="txt">当前鲜花数</div>
        </div>
        <div class="item">
          <div class="num">{{matchObj.rank}}</div>
          <div class="txt">排名</div>
        </div>
        <div class="item">
          <div class="num">{{matchObj.from_front_num}}</div>
          <div class="txt">距上一名差</div>
        </div>
      </div>
      <div class="btn">
        <div class="send-btn fs-32" @click="showModel()">送花给TA</div>
        <div class="share-btn fs-32" @click="toShare">分享拉票</div>
      </div>
    </div>
    <!-- 主要信息部分 -->
    <div class="main-mes">
      <div class="video-box">
        <video :src="matchObj.video" controls :poster="matchObj.video_photo"></video>
      </div>
    </div>
    <!-- 送花模态框 -->
    <div class="model" v-show="isShowModel" @touchmove.prevent @click.stop="closeFlowerModel">
      <div class="main-mes" @click.stop="mainValue">
        <div class="flower">
          <input
            type="number"
            placeholder="请输入送花数量"
            oninput="value=value.replace(/[^\d]/g,'')"
            v-model="sendNum"
          />
          <span class="fs-32">支</span>
        </div>
        <div class="flower-num fs-32">
          您当前拥有
          <span>{{flowerMes.num}}</span>
          支鲜花
        </div>
        <div class="flower-btn" v-show="!isFree">
          <div class="pay fs-32" @click="buyFlower()">购买鲜花</div>
          <div class="send fs-32" v-show="flowerMes.num != 0" @click="sendFolwer()">送花</div>
          <div class="send active fs-32" v-show="flowerMes.num == 0">送花</div>
        </div>
        <div class="flower-btn-s fs-32" v-show="isFree">送花</div>
        <div class="close-btn" @click="closeFlowerModel">
          <img :src="closeModel" alt />
        </div>
      </div>
    </div>
    <shareModel v-show="leaderModal" @fn="getShareModel" :leaderModal="leaderModal"></shareModel>
  </div>
</template>

<script>
export default {
  name: "mall",
  data() {
    return {
      title: "参赛者姓名",
      isHome: true,
      isBack: true,
      id: 0, //活动id
      enter_activity_id: 0, //参赛者id
      matchObj: {}, //参赛者信息
      flowerMes: {
        num: 8
      },
      sendNum: null, //送花的数量
      isShowModel: false, //送花model的显示和隐藏
      isFree: false, //不是免费
      closeModel: require("@/assets/index/close-btn.png"),
      leaderModal: false //控制微信内分享模态框
    };
  },
  watch: {
    sendNum: function() {
      if (this.sendNum.charAt(0) == 0) {
        this.sendNum = null;
      }
    }
  },
  components: {},
  methods: {
    // 去分享
    toShare() {
      console.log(this.matchObj, "---this.matchObj");
      this.is_weixn();
      // 记录进入app时的url
      if (typeof window.entryUrl === "undefined" || window.entryUrl === "") {
        window.entryUrl = location.href.split("#")[0];
      }
      if (this.isapp == 0) {
        this.wxshare.getWxmessage(
          this.matchObj.name,
          `排名` + this.matchObj.rank,
          /(Android)/i.test(navigator.userAgent)
            ? location.href.split("#")[0]
            : window.entryUrl,
          this.matchObj.user_avator,
          location.href
        );
      } else if (this.isapp == 1) {
        var sendData = {
          type: "1",
          data: {
            title: this.matchObj.name,
            description: `排名` + this.matchObj.rank,
            shareimg:  this.matchObj.user_avator,
            share_url: location.href
          }
        };
        window.postMessage(JSON.stringify(sendData));
      }
    },
    // 判断是否在微信中打开
    is_weixn() {
      var ua = navigator.userAgent.toLowerCase();
      if (ua.match(/MicroMessenger/i) == "micromessenger") {
        // 表示在微信里面
        this.leaderModal = true;
        return true;
      } else {
        return false;
      }
    },
    // 获取分享模态框传过来的status
    getShareModel(e) {
      this.leaderModal = e;
    },
    // 获取我所拥有的鲜花数量
    getFlowerNumber() {
      const url =
        this.$host1 +
        `home/audition/flowerNum?audition_activity_vote_time_id=` +
        this.id;
      this.$axios
        .get(url)
        .then(res => {
          console.log(res.data.data, "---res.data.data--");
          if (res.data.code == 200) {
            this.flowerMes.num = res.data.data.flower_num;
          } else {
            this.AlertWin(res.data.message);
          }
        })
        .catch(err => {
          console.log(err, "err-----");
        });
    },
    // 获取参赛者详情信息
    getMatchDetail() {
      const url =
        this.$host1 +
        `home/audition/enterPersonCenter?enter_activity_id=` +
        this.enter_activity_id;
      this.$axios
        .get(url)
        .then(res => {
          // console.log(res.data, "---res.data----");
          if (res.data.code == 200) {
            this.matchObj = res.data.data;
          } else {
            this.AlertWin(res.data.message);
          }
        })
        .catch(err => {
          console.log(err, "err-----");
        });
    },
    // 购买鲜花
    buyFlower() {
      this.$router.push({
        path: "/index/buy-flower",
        query: {}
      });
    },
    // 送花
    sendFolwer() {
      if (this.sendNum == null || this.sendNum == 0) {
        this.AlertWin("请输入正确的鲜花数量");
      } else if (this.sendNum > this.flowerMes.num) {
        this.AlertWin("您所拥有的鲜花数量不足，快去购买吧");
      } else {
        this.sendFlowerFn();
      }
    },
    // 送花接口
    sendFlowerFn() {
      const url = this.$host1 + `home/audition/sendFlower`;
      var params = {
        audition_activity_vote_time_id: this.id,
        enter_activity_id: this.enter_activity_id,
        send_flower_num: this.sendNum
      };
      this.$axios
        .post(url, params)
        .then(res => {
          console.log(res.data.data, "---res.data.data--");
          if (res.data.code == 200) {
            this.sendNum = null;
            this.closeFlowerModel();
          } else {
            this.AlertWin(res.data.message);
          }
        })
        .catch(err => {
          console.log(err, "err-----");
        });
    },
    // 解决关掉模态框问题
    mainValue() {},
    // 关掉模态框
    closeFlowerModel() {
      this.isShowModel = false;
      this.getMatchDetail(); //获取参赛者详情信息
      this.getFlowerNumber(); //获取我所拥有的鲜花数量
    },
    // 打开送花模态框
    showModel() {
      this.isShowModel = true;
    }
  },
  created() {
    this.enter_activity_id = this.$route.query.id;
    console.log(this.enter_activity_id, "---this.enter_activity_id");
    this.id = this.$route.query.activityId;
    console.log(this.id, "---id");
    this.getMatchDetail(); //获取参赛者详情信息
    this.getFlowerNumber(); //获取我所拥有的鲜花数量
  }
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style lang="scss" scoped>
.container {
  padding-top: 1rem;
  box-sizing: border-box;
  .top-card {
    padding: 0 0.24rem 0 0.27rem;
    box-sizing: border-box;
    height: 1.85rem;
    width: 100%;
    background: linear-gradient(to right, #680dae, #9529c6);
    display: flex;
    align-items: center;
    justify-content: space-between;
    .left {
      display: flex;
      align-items: center;
      .img {
        width: 1.1rem;
        height: 1.1rem;
        border-radius: 50%;
        margin-right: 0.31rem;
        overflow: hidden;
        img {
          width: 100%;
        }
      }
      .name {
        font-weight: bold;
        color: rgba(255, 255, 255, 1);
      }
    }
    .right {
      font-weight: 500;
      color: rgba(255, 255, 255, 1);
    }
  }
  .center-option {
    width: 100%;
    background-color: #fff;
    padding: 0.7rem 0 0.24rem;
    box-sizing: border-box;
    margin-bottom: 0.24rem;
    .top {
      display: flex;
      justify-content: space-around;
      align-items: center;
      margin-bottom: 0.49rem;
      .item {
        text-align: center;
        .num {
          font-size: 0.36rem;
          font-weight: bold;
          color: rgba(68, 68, 68, 1);
          margin-bottom: 0.21rem;
        }
        .txt {
          font-size: 0.26rem;
          font-weight: 500;
          color: rgba(153, 153, 153, 1);
        }
      }
    }
    .btn {
      display: flex;
      justify-content: space-around;
      align-items: center;
      .send-btn,
      .share-btn {
        width: 3.06rem;
        height: 0.7rem;
        background: rgba(244, 67, 73, 1);
        border-radius: 0.06rem;
        text-align: center;
        line-height: 0.75rem;
        font-weight: bold;
        color: rgba(255, 255, 255, 1);
      }
      .share-btn {
        background: rgba(84, 4, 121, 1);
      }
    }
  }
  .main-mes {
    padding: 0.24rem;
    box-sizing: border-box;
    background-color: #fff;
    .video-box {
      width: 100%;
      height: 8rem;
      video {
        width: 100%;
        height: 100%;
      }
    }
  }
  // 模态框样式设置
  .model {
    width: 100%;
    height: 100vh;
    position: fixed;
    z-index: 1000;
    left: 0;
    top: 0;
    right: 0;
    bottom: 0;
    background-color: rgba(0, 0, 0, 0.5);
    .main-mes {
      width: 6.77rem;
      height: 4.6rem;
      background-color: #fff;
      position: absolute;
      left: 50%;
      top: 25%;
      transform: translate(-50%);
      padding: 0.7rem 0.4rem 0;
      border-radius: 0.08rem;
      box-sizing: border-box;
      .flower {
        position: relative;
        margin-bottom: 0.43rem;
        input {
          width: 5.98rem;
          height: 0.88rem;
          border: 0.01rem solid rgba(153, 153, 153, 1);
          border-radius: 0.06rem;
          text-indent: 0.26rem;
        }
        input::-webkit-input-placeholder {
          font-size: 0.32rem;
          font-weight: 500;
          color: rgba(153, 153, 153, 1);
        }
        span {
          position: absolute;
          right: 0.26rem;
          top: 0.25rem;
          font-weight: 500;
          color: rgba(68, 68, 68, 1);
        }
      }

      .flower-num {
        font-weight: 500;
        color: rgba(68, 68, 68, 1);
        line-height: 0.32rem;
        margin-bottom: 0.79rem;
        span {
          color: #f44349;
        }
      }

      .flower-btn {
        display: flex;
        justify-content: space-between;
        .pay,
        .send {
          width: 2.84rem;
          height: 0.84rem;
          background: rgba(244, 67, 73, 1);
          border-radius: 0.06rem;
          text-align: center;
          line-height: 0.84rem;
          font-weight: bold;
          color: rgba(255, 255, 255, 1);
        }
        .active {
          background: rgba(245, 245, 245, 1);
          color: #444;
        }
      }
      .flower-btn-s {
        width: 100%;
        height: 0.84rem;
        background: rgba(244, 67, 73, 1);
        border-radius: 0.06rem;
        text-align: center;
        line-height: 0.84rem;
        font-weight: bold;
        color: rgba(255, 255, 255, 1);
      }
      .close-btn {
        width: 0.97rem;
        height: 0.97rem;
        position: absolute;
        bottom: -1.72rem;
        left: 50%;
        transform: translate(-50%);
        img {
          width: 100%;
        }
      }
    }
  }
}
</style>
